package br.com.dev.tcc.view.sinistro.chain.atropelamento;

import android.content.Context;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import br.com.dev.tcc.R;
import br.com.dev.tcc.bean.sinistro.GravidadeAcidente;
import br.com.dev.tcc.bean.sinistro.Sinistro;
import br.com.dev.tcc.bean.sinistro.Vitima;
import br.com.dev.tcc.singleton.SinistroSingleton;
import br.com.dev.tcc.util.CpfUtil;
import br.com.dev.tcc.view.sinistro.chain.SinistroChainItem;

public class SinistroAtropelamentoPasso3 extends SinistroChainItem {

	private View txtMachucou;
	private RadioButton radioMachucouSim;
	private View layoutMachucou;
	private RadioButton radioLesao;
	private RadioButton radioResgate;
	private RadioButton radioVitimaResponsavelSim;
	private EditText etNomeVitima;
	private EditText etCpfVitima;
	private EditText etTelVitima;

	public SinistroAtropelamentoPasso3(Context context) {
		super(context);
		inflateView(R.layout.sinistro_tipo_atropelamento3);
	}

	@Override
	public void init() {

		txtMachucou = view.findViewById(R.id.txtMachucou);
		layoutMachucou = view.findViewById(R.id.layoutMachucou);
		radioMachucouSim = (RadioButton) view
				.findViewById(R.id.radioMachucouSim);
		radioMachucouSim
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {

						if (isChecked)
							layoutMachucou.setVisibility(View.VISIBLE);
						else
							layoutMachucou.setVisibility(View.GONE);
					}
				});
		radioVitimaResponsavelSim = (RadioButton) view
				.findViewById(R.id.radioVitimaResponsavelSim);
		radioLesao = (RadioButton) view.findViewById(R.id.radioLesao);
		radioResgate = (RadioButton) view.findViewById(R.id.radioInternacaoSim);

		etCpfVitima = (EditText) view.findViewById(R.id.txtValorCpfVitima);
		etNomeVitima = (EditText) view.findViewById(R.id.txtValorNomeVitima);
		etTelVitima = (EditText) view.findViewById(R.id.txtValorTelVitima);
	}

	@Override
	public boolean isValid() {

		boolean isValid = true;

		if (etTelVitima.getText().toString().trim().length() < 8) {

			etTelVitima.setCompoundDrawablesWithIntrinsicBounds(null, null,
					context.getResources().getDrawable(R.drawable.exclamation),
					null);
			etTelVitima.requestLayout();
			etTelVitima.requestFocus();
			isValid = false;
		} else
			etCpfVitima.setCompoundDrawablesWithIntrinsicBounds(null, null,
					null, null);

		if (!CpfUtil.isValid(context, etCpfVitima)) {
			etCpfVitima.requestLayout();
			etCpfVitima.requestFocus();
			isValid = false;
		}

		if (etNomeVitima.getText().toString().trim().length() == 0) {

			etNomeVitima.setCompoundDrawablesWithIntrinsicBounds(null, null,
					context.getResources().getDrawable(R.drawable.exclamation),
					null);
			etNomeVitima.requestLayout();
			etNomeVitima.requestFocus();
			isValid = false;
		} else
			etNomeVitima.setCompoundDrawablesWithIntrinsicBounds(null, null,
					null, null);

		return isValid;
	}

	@Override
	public void setSinistroFields() {

		Sinistro sinistro = SinistroSingleton.getInstance();
		if (radioMachucouSim.isChecked()) {
			sinistro.setAlguemSeMachucou(true);
			sinistro.setGravidadeAcidente(radioLesao.isChecked() ? GravidadeAcidente.LESAO
					: GravidadeAcidente.OBITO);
			sinistro.setInternacaoOuResgate(radioResgate.isChecked());
		} else {
			sinistro.setAlguemSeMachucou(false);
			sinistro.setGravidadeAcidente(null);
		}
		sinistro.setVitimaAssumiuResponsabilidade(radioVitimaResponsavelSim
				.isChecked());

		Vitima vitima = new Vitima();
		vitima.setNome(etNomeVitima.getText().toString());
		vitima.setCpf(CpfUtil.getFormattedCpf(etCpfVitima.getText().toString()));
		vitima.setTelefone(etTelVitima.getText().toString());
		sinistro.setVitima(vitima);
	}

	@Override
	public View getFirstView() {
		return txtMachucou;
	}

	@Override
	public void onShow() {
		txtMachucou.requestFocus();
	}
}
